<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\AuthGroupAccessApi;
use Admin\Api\CustomerApi;
use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Admin\Api\MessageApi;
use Admin\Api\MessageBoxApi;
use Admin\Api\PhoneDeviceApi;
use Admin\Model\MessageModel;
use Api\Vendor\UMeng\BoyePushApi;
use Uclient\Api\UserApi;
use Shop\Api\MemberConfigApi;

class CustomerController extends AdminController {

	public function index() {
		$params = array();
		
		$map['nickname'] = array('like', "%" . I('nickname', '', 'trim') . "%");		
		$map['id'] = I('nickname',-1);
		$map['_logic'] = 'OR';
		
		$page = array('curpage' => I('get.p'), 'size' => 15);
		$order = "id";
		$params['nickname'] = I('nickname','','trim');
		$result = apiCall(CustomerApi::QUERY, array($map, $page, $order,$params));

		if ($result['status']) {
			
			$this -> assign("show", $result['info']['show']);
			$this -> assign("list", $result['info']['list']);
			$this -> display();
		} else {
			$this -> error($result['info']);
		}
	}
	
	/**
	 * 实名审核
	 */
	public function realname(){
		$page = array('curpage' => I('get.p'), 'size' => 15);
		$map=array('identity_validate'=>2);
		$result=apiCall(MemberConfigApi::QUERY,array($map,$page));
		$this->assign('member',$result['info']['list']);
		$resu=apiCall(MemberApi::QUERY_NO_PAGING,array());
		$this->assign('user',$resu['info']);
		$this -> display();
	}
	/**
	 * 审核通过
	 */
	public function pass(){
		$map=array('uid'=>I('id',0));
		$entity=array('identity_validate'=>1);
		$result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
		if($result['status']){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
/**
	 * 审核失败
	 */
	public function fail(){
		$map=array('uid'=>I('id',0));
		$entity=array('identity_validate'=>0);
		$result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
		if($result['status']){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}

	/**
	 * pushMessage推送消息
	 */
	public function pushMessageAll(){
		$this->display();
	}
	public function pushMessage($uid=0){
		if(IS_POST){
			$message_type = I('post.type',0,'int');
			$uid = I('post.uid',0,'int');
			$pushAll = I('post.pushAll',false,'boolean');

			$content = I('post.content','');
			$title = I('post.title','');
			$summary = I('post.summary','');

			//推送消息
			if($message_type==1){
				vendor('BoyePushApi',APP_PATH.'Api/Vendor/Umeng/','.class.php');

				$BoyePushApi = new BoyePushApi();

				$param = array(
						'alert' => $content,
						'ticker' => $title,
						'title' => $title,
						'text' => $content
				);

				if($pushAll){
					$result = $BoyePushApi->sendAll($param);
				}else{
					$result = $BoyePushApi->send($uid,$param);
				}

				if($result['status']){

					//记录消息
					$name = $pushAll?MessageModel::MESSAGE_SYSTEM:MessageModel::MESSAGE_PUSH;
					$result = apiCall(DatatreeApi::GET_INFO,array(array('name'=>$name)));
					if($result['status']){
						$dtree_type = !empty($result['info'])?$result['info']['id']:0;
					}else{
						$dtree_type = 0;
					}

					if($pushAll){
						$to_id = -2;
					}else{
						$to_id = $uid;
					}

					$entity = array(
						'title'=>$title,
						'content'=>$content,
						'to_id'=>$to_id,
						'dtree_type'=>$dtree_type,
						'from_id'=>0,//0:系统
						'summary'=>$summary,
						'send_time'=>time(),
						'status'=>1
					);
					$result = apiCall(MessageApi::RECORD_MESSAGE,array($entity));

					if($result['status']){
						$this->success('发送成功！',U('Admin/Message/index',array('uid'=>$uid)));
					}else{
						$this->error($result['info']);
					}

				}else{
					$this->error($result['info']);
				}
			}

			//其他消息
			if($message_type==2){

				//记录消息
				//记录消息
				$name = $pushAll?MessageModel::MESSAGE_SYSTEM:'其他消息';
				$result = apiCall(DatatreeApi::GET_INFO,array(array('name'=>$name)));
				if($result['status']){
					$dtree_type = !empty($result['info'])?$result['info']['id']:0;
				}else{
					$dtree_type = 0;
				}

				if($pushAll){
					$to_id = -2;
				}else{
					$to_id = $uid;
				}

				$entity = array(
					'title'=>$title,
					'content'=>$content,
					'to_id'=>$to_id,
					'dtree_type'=>$dtree_type,
					'from_id'=>0,//0:系统
					'summary'=>$summary,
					'send_time'=>time(),
					'status'=>1
				);
				$result = apiCall(MessageApi::RECORD_MESSAGE,array($entity));

				if($result['status']){
					$this->success('发送成功！',U('Admin/Message/index',array('uid'=>$uid)));
				}else{
					$this->error($result['info']);
				}
			}



		}

		$this->display();
	}
	
	/**
	 * 检测用户名是否已存在
	 */
	public function check_username($username){
		$result = apiCall(UserApi::CHECK_USER_NAME,array($username));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}
	
	 /**
	 * 检测用户名是否已存在
	 */
	public function check_email(){
			$result = apiCall(UserApi::CHECK_EMAIL,array($email));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}
	
	/**
	 * 
	 */
	public function select(){
			
		$map['nickname'] = array('like', "%" . I('q', '', 'trim') . "%");		
		$map['uid'] = I('q',-1);
		$map['_logic'] = 'OR';
		$page = array('curpage'=>0,'size'=>20);
		$order = " last_login_time desc ";
		
		$result = apiCall(MemberApi::QUERY, array($map,$page, $order,false,'uid,nickname,head'));

		if($result['status']){
			$list = $result['info']['list'];
			
			foreach($list as $key=>$g){

				$list[$key]['id']=$list[$key]['uid'];
                $list[$key]['head'] = getImageUrl($list[$key]['head']);
			}
			
			$this->success($list);
		}	
	
	}

    public function view(){
        $id = I('get.id',0);

        $result = apiCall(MemberApi::GET_INFO, array(array("uid"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userinfo",$result['info']);

        $result = apiCall(UserApi::GET_INFO, array($id));

        if($result['status']){
			if(!is_array($result['info'])){
				$this->error($result['info']);
			}
		}else{
			$this->error('未知错误');
		}

        $this->assign("useraccount",$result['info']);

        $this->display();
    }
	
}
